package com.xx.account.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

import com.xx.account.domain.AccountRecord;
import com.xx.account.mapper.AccountRecordMapper;
import com.xx.common.exception.ServiceException;
import com.xx.common.utils.DateUtils;
import com.xx.common.utils.SecurityUtils;
import com.xx.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xx.account.mapper.AccountBossMapper;
import com.xx.account.domain.AccountBoss;
import com.xx.account.service.IAccountBossService;

import javax.annotation.Resource;

/**
 * 老板Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-28
 */
@Service
public class AccountBossServiceImpl implements IAccountBossService
{
    @Resource
    private AccountBossMapper accountBossMapper;

    @Resource
    private AccountRecordMapper accountRecordMapper;

    /**
     * 查询老板
     *
     * @param id 老板主键
     * @return 老板
     */
    @Override
    public AccountBoss selectAccountBossById(String id)
    {
        return accountBossMapper.selectAccountBossById(id);
    }

    /**
     * 查询老板列表
     *
     * @param accountBoss 老板
     * @return 老板
     */
    @Override
    public List<AccountBoss> selectAccountBossList(AccountBoss accountBoss)
    {
        accountBoss.setCreateBy(SecurityUtils.getUserId().toString());
        return accountBossMapper.selectAccountBossList(accountBoss);
    }

    /**
     * 新增老板
     *
     * @param accountBoss 老板
     * @return 结果
     */
    @Override
    public int insertAccountBoss(AccountBoss accountBoss)
    {
        accountBoss.setId(UUID.randomUUID().toString().replaceAll("-",""));
        accountBoss.setCount(0L);
        accountBoss.setCreateTime(DateUtils.getNowDate());
        accountBoss.setCreateBy(SecurityUtils.getUserId().toString());
        return accountBossMapper.insertAccountBoss(accountBoss);
    }

    /**
     * 修改老板
     *
     * @param accountBoss 老板
     * @return 结果
     */
    @Override
    public int updateAccountBoss(AccountBoss accountBoss)
    {
        return accountBossMapper.updateAccountBoss(accountBoss);
    }

    /**
     * 批量删除老板
     *
     * @param ids 需要删除的老板主键
     * @return 结果
     */
    @Override
    public int deleteAccountBossByIds(String[] ids)
    {
        // 删除前先查询选中的数据是否被引用，引用则不让删除
        StringBuilder sb = new StringBuilder();
        for (String bossId : ids) {
            AccountRecord accountRecord = new AccountRecord();
            accountRecord.setBossId(bossId);
            List<AccountRecord> list = accountRecordMapper.selectAccountRecordListByBossId(accountRecord);
            if(!list.isEmpty()){
                sb.append("【").append(list.get(0).getBossName()).append("】");
            }
        }

        if(StringUtils.isNotEmpty(sb)){
            throw new ServiceException("老板名称：" + sb + "已经被引用，不允许删除！");
        }

        return accountBossMapper.deleteAccountBossByIds(ids);
    }

    /**
     * 删除老板信息
     *
     * @param id 老板主键
     * @return 结果
     */
    @Override
    public int deleteAccountBossById(String id)
    {
        return accountBossMapper.deleteAccountBossById(id);
    }
}
